package GradeManager.StudentGradeSystem.controller;

import GradeManager.StudentGradeSystem.pojo.PersonInfo;
import GradeManager.StudentGradeSystem.pojo.Result;
import GradeManager.StudentGradeSystem.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;
@Slf4j
@RestController
@RequestMapping("/addInfo")
public class InfoController {
    @Value("${image-address.basepath}")
    private String basePath;
    @Autowired
    private PersonService service;
    @PostMapping
    public Result addinfo(@RequestBody PersonInfo infoData){
        service.addInfo(infoData);
        return Result.success();
    }
    @PostMapping("/upload")
    public Result handleimage(MultipartFile file) throws IOException {
//        获取上传的文件名
        String orginalfilename=file.getOriginalFilename();
//        获取文件名后缀
        String suffix=orginalfilename.substring(orginalfilename.lastIndexOf("."));
//        判断转存的目录是否存在
        File dir=new File(basePath);
        if(!dir.exists()){
//            不存在则创建文件夹
            dir.mkdirs();
        }
//        根据UUID创建新的文件名
        String filename=UUID.randomUUID()+suffix;
//        图片转存到新建的文件夹中
        file.transferTo(new File(basePath+filename));
        log.info("图片转存的地址:{}",basePath+filename);
//        返回文件夹的名称
        return Result.success(filename);
    }
    @GetMapping("/download")
    public void download(String filename, HttpServletResponse resp) throws IOException {
//        获取输出流，将转存的图片写回页面中
        ServletOutputStream outputStream = resp.getOutputStream();
//        设置写回的格式为image/jpeg类型
        resp.setContentType("image/jpeg");
//        用字节流写回图片
        FileInputStream inputStream=new FileInputStream(basePath+filename);
        byte[] b=new byte[1024];
        int len;
        while((len=inputStream.read(b))!=-1){
            outputStream.write(b,0,len);
            outputStream.flush();
        }
//        关闭流
        inputStream.close();
        outputStream.close();
    }
    @GetMapping("/getInfo")
    public Result getInfo(){
        //判断个人信息是否已添加
        if (service.getInfo()){
            return Result.error("信息已存在！");
        }else return Result.success();
    }
    @GetMapping("/lookInfo")
    public Result lookInfo(HttpServletRequest request){
        PersonInfo personInfo = service.lookInfo(request);
        if(personInfo.getUserImage()==null){
            return new Result(0,"Info_disabled",personInfo);
        }
        return Result.success(personInfo);
    }
    @PutMapping("/modifyInfo")
    public Result modifyInfo(@RequestBody PersonInfo infoData){
        service.modifyInfo(infoData);
        return Result.success();
    }
    @GetMapping("/reshow")
    public Result reshow(HttpSession session){
        PersonInfo modifiedInfo = service.reshow();
        return Result.success(modifiedInfo);
    }
}
